package com.test.service.impl;


import cn.hutool.core.util.ObjectUtil;
import com.alipay.api.domain.AlipayDataDataserviceBillDownloadurlQueryModel;
import com.alipay.api.domain.AlipayTradeQueryModel;
import com.alipay.api.domain.AlipayTradeRefundModel;
import com.alipay.api.domain.AlipayTradeWapPayModel;
import com.test.properties.AliPayProperties;
import com.test.service.AlipayService;
import com.test.utils.AliPayUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author mpk
 * @since 2024/9/13 15:09:44
 */
@Slf4j
@Service
public class AlipayServiceImpl implements AlipayService {

    @Autowired
    AliPayUtil alipayUtil;

    @Autowired
    AliPayProperties aliPayProperties;
    public String appPay(String manageId) {

        AlipayTradeWapPayModel alipayTradeWapPayModel = new AlipayTradeWapPayModel();
        //TODO 填写支付参数
        // out_trade_no , total_amount, subject, product_code
        alipayTradeWapPayModel.setOutTradeNo(manageId);
        alipayTradeWapPayModel.setTotalAmount("20.00");
        alipayTradeWapPayModel.setSubject("商品");
        alipayTradeWapPayModel.setProductCode("QUICK_WAP_WAY");

        String string = alipayUtil.appPay(alipayTradeWapPayModel);
        return string;
    }

    @Override
    public void refund(String manageId) {
        AlipayTradeRefundModel alipayTradeRefundModel = new AlipayTradeRefundModel();
        //TODO 填写退款参数
        //  out_trade_no,trade_no 二选一
        alipayTradeRefundModel.setOutTradeNo(manageId);
        alipayTradeRefundModel.setRefundAmount("20.00");
//        alipayTradeRefundModel.setTradeNo();
        String refund = alipayUtil.refund(alipayTradeRefundModel);
        log.info(refund);
    }

    @Override
    public String bill() {
        AlipayDataDataserviceBillDownloadurlQueryModel queryModel = new AlipayDataDataserviceBillDownloadurlQueryModel();
        //TODO 填写查询参数
        // bill_type,bill_date 必选
        queryModel.setBillType("trade");
        queryModel.setBillDate("20230913");
        String bill = alipayUtil.bill(queryModel);
        log.info("退款结果：" + bill);

        return bill;
    }

    @Override
    public String tradeQuery(HashMap<String, Object> map) {
        AlipayTradeQueryModel queryModel = new AlipayTradeQueryModel();
        //TODO 填写查询参
        // out_trade_no, trade_no 二选一
        //2024091322001412670503776569
        //213123
        if(ObjectUtil.isNotEmpty(map.get("out_trade_no")) )
        {
            queryModel.setTradeNo(map.get("out_trade_no").toString());
        }
        if(ObjectUtil.isNotEmpty(map.get("trade_no")) )
        {
            queryModel.setOutTradeNo(map.get("trade_no").toString());
        }
        // 设置查询选项
        List<String> queryOptions = new ArrayList<>();
        queryOptions.add("trade_settle_info");

        queryModel.setQueryOptions(queryOptions);
        String string = alipayUtil.queryTrade(queryModel);
        log.info("账单查询结果：" + string);
        return string;
    }

    @Override
    public void notify(HttpServletRequest request) {
        Map<String, String> map = alipayUtil.notifyServer(request);
        //TODO 处理成功或失败的逻辑
//        tradeStatus.TRADE_CLOSED	交易关闭	1
//        tradeStatus.TRADE_FINISHED	交易完结	1
//        tradeStatus.TRADE_SUCCESS	支付成功	1
//        tradeStatus.WAIT_BUYER_PAY	交易创建	0
        log.info(map.get("trade_status"));

        log.info("通知结果：" + map);

    }
}
